package com.lc.ibps.platform.rest.org;

import java.util.List;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lc.ibps.api.base.model.PartyEntity;
import com.lc.ibps.api.base.model.PartyRole;
import com.lc.ibps.api.base.query.QueryFilter;
import com.lc.ibps.api.base.query.QueryOP;
import com.lc.ibps.api.org.model.PartyRoleTree;
import com.lc.ibps.api.org.service.IPartyRoleService;
import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.base.core.util.string.StringUtil;
import com.lc.ibps.base.db.mybatis.domain.DefaultPage;
import com.lc.ibps.cloud.entity.APIPageList;
import com.lc.ibps.cloud.entity.APIRequest;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.org.party.persistence.entity.PartyRolePo;
import com.lc.ibps.org.party.repository.PartyRoleRepository;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 角色服务 控制器，第三方系统调用。
 *
 * <pre>
 *  
 * 构建组：ibps-platform-webapi
 * 作者：eddy
 * 邮箱：1546077710@qq.com
 * 日期：2016年9月22日-下午2:47:07
 * 版权：广州流辰信息技术有限公司版权所有
 * </pre>
 */

@RequestMapping(value = "/api/webapi/roleService")
@Api(value = "/roleService", tags = "用户管理-角色服务")
@RestController
public class RoleController extends BaseApiController {

	private IPartyRoleService roleService;
	private PartyRoleRepository partyRoleRepository;

	public RoleController() {
		roleService = AppUtil.getBean(IPartyRoleService.class);
		partyRoleRepository = AppUtil.getBean(PartyRoleRepository.class);
	}

	@ApiOperation(value = "根据ID获取角色", notes = "根据ID获取角色")
	@RequestMapping(value = "/loadById", method = RequestMethod.GET)
	public APIResult<PartyRole> loadById(
			@ApiParam(value = "id", required = true) @RequestParam(name = "id", required = true) String id) {
		APIResult<PartyRole> result = new APIResult<>();
		try {
			PartyRole role = roleService.getById(id);
			result.setData(role);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取用户信息失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据别名获取角色", notes = "根据别名获取角色")
	@RequestMapping(value = "/loadByAlias", method = RequestMethod.GET)
	public APIResult<PartyRole> loadByAlias(
			@ApiParam(value = "alias", required = true) @RequestParam(name = "alias", required = true) String alias) {
		APIResult<PartyRole> result = new APIResult<>();
		try {
			PartyRole role = roleService.getByAlias(alias);
			result.setData(role);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取用户信息失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "获取角色列表", notes = "获取角色列表")
	@RequestMapping(value = "/findAll", method = RequestMethod.GET)
	public APIResult<List<PartyRole>> findAll() {

		APIResult<List<PartyRole>> result = new APIResult<>();
		try {
			List<PartyRole> role = roleService.findAll();
			result.setData(role);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取用户信息失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据用户账号获取角色列表", notes = "根据用户账号获取角色列表")
	@RequestMapping(value = "/findByUserAccount", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findByUserAccount(
			@ApiParam(value = "account", required = true) @RequestParam(name = "account", required = true) String account) {
		APIResult<List<PartyEntity>> result = new APIResult<>();
		try {
			List<PartyEntity> role = roleService.getByUserAccount(account);
			result.setData(role);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取用户信息失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据用户ID获取角色列表", notes = "根据用户ID获取角色列表")
	@RequestMapping(value = "/findByUserId", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findByUserId(
			@ApiParam(value = "userId", required = true) @RequestParam(name = "userId", required = true) String userId) {
		APIResult<List<PartyEntity>> result = new APIResult<>();
		try {
			List<PartyEntity> role = roleService.getByUserId(userId);
			result.setData(role);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取用户信息失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据子系统ID获取角色列表", notes = "根据子系统ID获取角色列表")
	@RequestMapping(value = "/queryBySubSysId", method = RequestMethod.GET)
	public APIResult<List<PartyRole>> queryBySubSysId(
			@ApiParam(value = "subSysId", required = true) @RequestParam(name = "subSysId", required = true) String subSysId,
			@ApiParam(value = "page", required = false, defaultValue = "1") @RequestParam(name = "page", required = true) int page,
			@ApiParam(value = "limit", required = false, defaultValue = "20") @RequestParam(name = "limit", required = true) int limit) {
		
		APIResult<List<PartyRole>> result = new APIResult<>();
		try {
			List<PartyRole> role = roleService.queryBySubSysId(subSysId, new DefaultPage(page, limit));
			result.setData(role);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取用户信息失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据子系统ID获取角色列表", notes = "根据子系统ID获取角色列表")
	@RequestMapping(value = "/findBySubSysId", method = RequestMethod.GET)
	public APIResult<List<PartyRole>> findBySubSysId(
			@ApiParam(value = "subSysId", required = true) @RequestParam(name = "subSysId", required = true) String subSysId) {
		APIResult<List<PartyRole>> result = new APIResult<>();// TODO 没报错
																// 子系统设不了角色
		try {
			List<PartyRole> role = roleService.findBySubSysId(subSysId);
			result.setData(role);

		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取数据失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据子系统别名获取角色列表", notes = "根据子系统别名获取角色列表")
	@RequestMapping(value = "/queryBySubSysAlias", method = RequestMethod.GET)
	public APIResult<List<PartyRole>> queryBySubSysAlias(
			@ApiParam(value = "subSysAlias", required = true) @RequestParam(name = "subSysAlias", required = true) String subSysAlias,
			@ApiParam(value = "page", required = false, defaultValue = "1") @RequestParam(name = "page", required = false) int page,
			@ApiParam(value = "limit", required = false, defaultValue = "20") @RequestParam(name = "limit", required = false) int limit) {
		APIResult<List<PartyRole>> result = new APIResult<>();
		try {
			List<PartyRole> role = roleService.queryBySubSysAlias(subSysAlias, new DefaultPage(page, limit));
			result.setData(role);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取数据失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据子系统别名获取角色列表", notes = "根据子系统别名获取角色列表")
	@RequestMapping(value = "/findBySubSysAlias", method = RequestMethod.GET)
	public APIResult<List<PartyRole>> findBySubSysAlias(
			@ApiParam(value = "subSysAlias", required = true) @RequestParam(name = "subSysAlias", required = true) String subSysAlias) {
		APIResult<List<PartyRole>> result = new APIResult<>();
		try {
			List<PartyRole> role = roleService.findBySubSysAlias(subSysAlias);
			result.setData(role);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取数据失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "获取角色列表", notes = "获取角色树数据")
	@RequestMapping(value = "/findTreeData", method = RequestMethod.GET)
	public APIResult<List<PartyRoleTree>> findTreeData() {
		APIResult<List<PartyRoleTree>> result = new APIResult<>();
		try {
			List<PartyRoleTree> role = roleService.findTreeData();
			result.setData(role);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取数据失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据子系统ID获取角色树数据", notes = "根据子系统ID获取角色树数据")
	@RequestMapping(value = "/findTreeDataBySubSysId", method = RequestMethod.GET)
	public APIResult<List<PartyRoleTree>> findTreeDataBySubSysId(
			@ApiParam(value = "subSysId", required = true) @RequestParam(name = "subSysId", required = true) String subSysId) {
		
		APIResult<List<PartyRoleTree>> result = new APIResult<>();
		try {
			List<PartyRoleTree> role = roleService.findTreeDataBySubSysId(subSysId);
			result.setData(role);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取数据失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据子系统别名获取角色树数据", notes = "根据子系统别名获取角色树数据")
	@RequestMapping(value = "/findTreeDataBySubSysAlias", method = RequestMethod.GET)
	public APIResult<List<PartyRoleTree>> findTreeDataBySubSysAlias(
			@ApiParam(value = "subSysAlias", required = true) @RequestParam(name = "subSysAlias", required = true) String subSysAlias) {

		APIResult<List<PartyRoleTree>> result = new APIResult<>();
		try {
			List<PartyRoleTree> role = roleService.findTreeDataBySubSysAlias(subSysAlias);
			result.setData(role);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取数据失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据子系统ID获取角色列表", notes = "根据子系统ID获取角色列表")
	@RequestMapping(value = "/queryBySubSysIdOrName", method = RequestMethod.POST)
	public APIResult<APIPageList<PartyRolePo>> queryBySubSysIdOrName(
			@ApiParam(name = "request", value = "传入查询请求json字符串", required = true) @RequestBody(required = true) APIRequest request) {
		
		APIResult<APIPageList<PartyRolePo>> result = new APIResult<>(); 
		QueryFilter queryFilter = getQuerFilter(request);
		if (StringUtil.isNotEmpty(request.getParameter("name"))) {
			queryFilter.addFilter("NAME_", "%" + request.getParameter("name") + "%", QueryOP.LIKE);
		} else if (StringUtil.isNotEmpty(request.getParameter("sysId"))) {
			queryFilter.addFilter("SYSTEM_ID_", request.getParameter("sysId"), QueryOP.EQUAL);
		}
		try {
			List<PartyRolePo> role = partyRoleRepository.queryWithSysBySysId(queryFilter);
			APIPageList<PartyRolePo> pageList = getAPIPageList(role);
			result.setData(pageList);

		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取数据失败！", e);
		}

		return result;
	}

}
